<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 6:54 PM
 */
declare(strict_types=1);


namespace application\module\admin\library\component;


use application\module\admin\addon\Rbca;
use application\module\admin\addon\Sign;
use application\module\admin\library\Component;

class SideMenuBar extends Component
{
    private $uid = null;

    private function getUserId(): int
    {
        if (!$this->uid) {
            $this->uid = Sign::getInstance()->getUser()->id;
        }
        return (int)$this->uid;
    }

    private function parseLink(array $link): string
    {
        if (!empty($link['value'])) {
            if (strpos($link['value'], '/') === 0 or strpos($link['value'], 'http') === 0) {
                $value = $link['value'];
            } else {
                $value = WS_PUBLIC_URL . '/' . $link['value'];
            }
        } else {
            $value = 'javascript:void(0);';
        }
        $icon = $link['icon']??'home';
        $name = $link['name']??'未命名';
        $tag = $link['tag']??'';
        $target = $link['target']??'_self';

        if (!empty($link['children'])) {
            $ul = $this->parseList($link['children']);
            if (trim($ul)) {
                return <<< endline
<li class="treeview">
    <a href="javascript:void(0);">
        <i class="fa fa-{$icon}"></i> 
        <span>{$name}</span>
        <span class="pull-right-container">
            <i class="fa fa-angle-left pull-right">{$tag}</i>
        </span>
    </a>
    {$ul}
</li>
endline;
            } else {
                # 没有子元素,隐藏
                return '';
            }
        } else {
            //TODO:如果没有权限,隐藏
            if (!Rbca::getInstance()->checkAuthorize($this->getUserId(), $link['value'])) {
                return '';
            } else {
                return <<< endline
<li>
    <a class='naz-sidebar-link' href="{$value}"  target="{$target}">
        <i class="fa fa-{$icon}"></i> 
        <span> {$name} </span>
        <span class="pull-right-container">
          <small class="label pull-right bg-green">{$tag}</small>
        </span>
    </a>
</li>
endline;
            }
        }
    }

    private function parseList(array $list): string
    {
        $sub = '';
        foreach ($list as $child) {
            $sub .= $this->parseLink($child);
        }
        return $sub ? "<ul class=\"treeview-menu\">{$sub}</ul>" : '';
    }

    public function __toString()
    {
        $content = '<ul id="naz-sidebar-menu" class="sidebar-menu">';
        if (!empty($this->attrs['menu'])) foreach ($this->attrs['menu'] as $submenu) {
            $content .= "<li class=\"header\">{$submenu['name']}</li>";
            foreach ($submenu['children'] as $child) {
                $content .= $this->parseLink($child);
            }
        }
        $location = WS_HOST_URL . $_SERVER['REQUEST_URI'];
        return $content . '</ul>' . <<<endline
<script>
    (function () {
        $("#naz-sidebar-menu").find(".naz-sidebar-link").each(function () {
            var link = $(this);
            if (link.attr("href") === "{$location}") {
                link.parent('li').addClass("active");
                link.parents("li.treeview").addClass("active");
            }
        });
    })();
</script>
endline;
    }
}

/*
    <ul class="sidebar-menu">
        <li class="header">HEADER</li>
        <!-- Optionally, you can add icons to the links -->
        <li class="active"><a href="javascript:void(0);"><i class="fa fa-link"></i> <span>Link</span></a></li>
        <li><a href="javascript:void(0);"><i class="fa fa-link"></i> <span>Another Link</span></a></li>
        <li class="treeview">
            <a href="javascript:void(0);"><i class="fa fa-link"></i> <span>Multilevel</span>
                <span class="pull-right-container">
      <i class="fa fa-angle-left pull-right"></i>
    </span>
            </a>
            <ul class="treeview-menu">
                <li><a href="javascript:void(0);">Link in level 2</a></li>
                <li><a href="javascript:void(0);">Link in level 2</a></li>
            </ul>
        </li>
    </ul>
*/